package com.yjt.fresh.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.HttpManager;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.listener.HttpOnNextListener;
import com.yjt.fresh.R;
import com.yjt.fresh.adpater.AdpRvAtyMassage;
import com.yjt.fresh.api.ApiGetMassages;
import com.yjt.fresh.base.BaseSimpleActivity;
import com.yjt.fresh.bean.BeanMessage;
import com.yjt.fresh.constant.MyConstants;
import com.yjt.fresh.utils.PreferenceManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 消息列表
 * Created by mcc on 2018-07-12.
 */

public class AtyMassages extends BaseSimpleActivity {
    private static final int PAGE_SIZE = 10;
    @BindView(R.id.back)
    RelativeLayout mBack;
    @BindView(R.id.rvAppAtyMassageList)
    RecyclerView mRv;
    @BindView(R.id.srlAppAtyMassage)
    SwipeRefreshLayout mRefreshLayout;
    private List<BeanMessage> mMassageList;
    private int mNextRequestPage = 1;
    private AdpRvAtyMassage mAdpMassage;
    private boolean isLoadMore;

    @Override
    protected int getContentView() {
        return R.layout.app_aty_massage_list;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
    }

    @Override
    protected void initEvent() {
        initRv();
        initClick();
        getData();
    }

    private void initRv() {
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        mRv.setLayoutManager(manager);
        mMassageList = new ArrayList<>();
        mAdpMassage = new AdpRvAtyMassage(R.layout.app_recycle_item_massage, mMassageList);
        mRv.setAdapter(mAdpMassage);
    }

    private void initClick() {
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mAdpMassage.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                BeanMessage beanMessage = mAdpMassage.getItem(position);
                Intent intent = new Intent(mContext,AtyMassageDetail.class);
                intent.putExtra(MyConstants.MASSAGE_CONTENT, beanMessage.getCONTENT());
                intent.putExtra(MyConstants.MASSAGE_TITLE,beanMessage.getTITLE());
                intent.putExtra(MyConstants.MASSAGE_ID,beanMessage.getAPPUSERMESSAGE_ID());
                startActivity(intent);
            }
        });

        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mNextRequestPage = 1;
                isLoadMore = false;
                getData();
            }
        });

        mAdpMassage.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                isLoadMore = true;
                getData();
            }
        },mRv);
    }


    HttpOnNextListener<List<BeanMessage>> mMassagesListener = new HttpOnNextListener<List<BeanMessage>>() {
        @Override
        public void onNext(List<BeanMessage> beanMessages) {
            if (isLoadMore) {
                setData(false, beanMessages);
            } else {
                /*for (int i = 0; i < 5; i++) {
                    BeanLiveCourse beanLiveCourse = new BeanLiveCourse();
                    beanLiveCourses.add(beanLiveCourse);
                }*/
                setData(true, beanMessages);
                mRefreshLayout.setRefreshing(false);
            }
        }

        @Override
        public void onReLogin() {
            toLogin();
        }
    };

    private void getData() {
        ApiGetMassages apiGetMassages = new ApiGetMassages(mMassagesListener, mContext, true, false, true);
        Map<String, Object> map = new HashMap<>();
        map.put("currentPage", mNextRequestPage);
        map.put("SESSION_ID", PreferenceManager.getInstance().getUserInfo().getSESSION_ID());
        apiGetMassages.setMap(map);
        HttpManager.getInstance().doHttpDeal(apiGetMassages);
    }

    private void setData(boolean isRefresh, List<BeanMessage> data) {
        mNextRequestPage++;
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
            mAdpMassage.setNewData(data);
        } else {
            if (size > 0) {
                mAdpMassage.addData(data);
                mAdpMassage.loadMoreComplete();
            }else {
                mAdpMassage.loadMoreEnd(true);
            }
        }
    }
}
